﻿using AutoMapper;
using MediatR;
using T.S.BaseData.Api.AppClinet.Commend.Goods;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Goods
{
    /// <summary>
    /// 添加商品信息
    /// </summary>
    public class GoodsAddCommandHandle : IRequestHandler<GoodsAddCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<GoodsInfo> goodsInfo;
        private readonly ILogger<GoodsAddCommandHandle> logger;
        private readonly IMapper mapper;

        public GoodsAddCommandHandle(IBaseRepository<GoodsInfo> goodsInfo, 
            ILogger<GoodsAddCommandHandle> logger, 
            IMapper mapper)
        {
            this.goodsInfo = goodsInfo;
            this.logger = logger;
            this.mapper = mapper;
        }
        
        
        public Task<ApiResult<int>> Handle(GoodsAddCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model = mapper.Map<GoodsInfo>(request);
            //model.GoodsStatus = false; EF全局过滤器 DbContext
             
            var r = goodsInfo.Crate(model);
            if (r>0)
            {
                result.Code = ApiEnums.Success;
                result.Data = r;
                result.Meg = "添加商品基本成功";

                logger.LogInformation("添加商品基本成功");
            }
            else
            {
                result.Code = ApiEnums.Error;
                result.Data = r;
                result.Meg = "添加商品基本失败";

                logger.LogInformation("添加商品基本失败");
            }
            return Task.FromResult(result);
        }
    }
}
